package com.example.hotelsystem.controller.userrolepower;


import code.CodeError;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.hotelsystem.entity.*;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.service.HotelorderService;
import com.example.hotelsystem.service.TCommentService;
import com.example.hotelsystem.service.UserService;
import com.example.hotelsystem.utils.ExcelUtils;
import com.example.hotelsystem.vo.user.UserSearchVO;
import com.example.hotelsystem.vo.user.UserUpdateVO;
import com.example.hotelsystem.vo.user.UserViewVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lee
 * @since 2020-10-13
 */
@RestController
@RequestMapping("/hotelsystem/user")
@Api(tags = "用户控制器")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    TCommentService tCommentService;
    @Autowired
    HotelorderService hotelorderService;

    @PostMapping("/view")
    @ApiOperation("查看用户的详情信息")
    public Result UserView(@RequestParam Long id){
        User user = userService.GetUserView(id);
        if(user!=null){
            UserViewVO userViewVO = new UserViewVO();
            BeanUtils.copyProperties(user,userViewVO);
            return Result.success(userViewVO);
        }else{
            return Result.error(CodeError.USER_VIEW_NULL,"没有找到相应的用户信息");
        }
    }

    @PostMapping("/managerView")
    @ApiOperation("后台---查看用户的详情信息")
    public Result managerUserView(@RequestParam Long id){
        User user = userService.GetUserView(id);
        if(user!=null){
            return Result.success(user);
        }else{
            return Result.error(CodeError.USER_VIEW_NULL,"没有找到相应的用户信息");
        }
    }



    @ApiOperation("获取所有用户列表接口")
    @PostMapping("/list")
    public Result userList(@RequestParam(defaultValue = "1") Integer currentPage){
        try {
            Result result = userService.userList(currentPage);
            return result;
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation("新增用户接口")
    @PostMapping("/add")
    public Result insertUser(User user){
        Result result = userService.insertUser(user);
        return result;
    }


    @ApiOperation("修改用户接口")
    @PostMapping("/update")
    public Result updateUser(UserUpdateVO vo){
        User user = new User();
        BeanUtils.copyProperties(vo,user);
        Result result = userService.updateUser(user);
        return result;
    }

    @ApiOperation("重置修改密码")
    @PostMapping("/changePassword")
    public Result changePassword(String email,String password){
        Result result = userService.changePassword(email,password);
        return result;
    }

    @ApiOperation("删除用户接口")
    @PostMapping("/remove")
    public Result removeUser(@RequestParam("id")Long id){
        boolean isRemove;
        User user = userService.getById(id);
        user.setIsDelete(1);
        isRemove=userService.updateById(user);
        if(isRemove){
//            redisTemplate.delete("user_"+id);
            return Result.success("删除成功");
        }else{
            return Result.error(CodeError.USER_REMOVE_FAIL,"删除失败");
        }
    }

    @ApiOperation("下载导入模板")
    @RequestMapping(value = "/install",method = RequestMethod.GET)
    public Result install(HttpServletResponse response) {
        try{
            List<User> list = new ArrayList<>();
            ExcelUtils.exportExcel(list, null, "用户数据", User.class, "导入模板.xlsx", response);
            return Result.success("成功");
        }catch (Exception e){
            return Result.error(CodeError.INTERNAL_EXCEPTION,"导出失败");
        }
    }


    @ApiOperation("批量导出全部用户信息表")
    @RequestMapping(value = "/export",method = RequestMethod.GET)
    public Result downLoadToExcel(HttpServletResponse response) {
        return userService.ExportExcelUserList(response);
    }


    @ApiOperation("使用Excel表批量导入用户")
    @PostMapping("/impUser")
    public Result impUser(MultipartFile file){
        return userService.ImportExcelUser(file);
    }


    @ApiOperation("以pdf形式导出个人信息")
    @ResponseBody
    @GetMapping("/pdfuser")
    public ModelAndView printUserPdf(Long id){
        return userService.ExportUserPdf(userService.getById(id));
    }

    @ApiOperation("根据多条件(用户名字、身份证、年龄、电话号码)模糊查找用户")
    @PostMapping("/search")
    public Result SearchUser(UserSearchVO vo){
        return Result.success("查询成功",userService.SearchUser(vo));
    }


    @ApiOperation("用户上传头像获得图片url")
    @PostMapping("/uploadImg")
    public Result UserUploadImg(MultipartFile file){
        return userService.UserUploadImg(file);
    }

}


